using UnityEngine;
using UnityEngine.InputSystem;

public class Lesson5 : MonoBehaviour
{
    Gamepad gamepad;
    void Start()
    {
        #region 知识点一 获取当前手柄
        gamepad = Gamepad.current;
        if (gamepad == null)
        {
            Debug.LogFormat("gamepad is nil");
            return;
        }
        #endregion

       
    }

    // Update is called once per frame
    void Update()
    {
        if (gamepad == null)
            return;
        #region 知识点二 手柄摇杆
        //摇杆方向
        //左摇杆
        // Debug.LogFormat("左摇杆:{0}", gamepad.leftStick.ReadValue());
        //右摇杆
        // Debug.LogFormat("左摇杆:{0}", gamepad.rightStick.ReadValue());
        //摇杆按下
        //右摇杆
        // gamepad.rightStickButton
        if (gamepad.leftStickButton.wasPressedThisFrame)
        {
            Debug.LogFormat("左摇杆 按下");
            Debug.LogFormat("左摇杆:{0}", gamepad.leftStick.ReadValue());
        }
        //摇杆抬起
        if (gamepad.leftStickButton.wasReleasedThisFrame)
        {
            Debug.LogFormat("左摇杆 抬起");
        }
        //摇杆长按
        if (gamepad.leftStickButton.isPressed)
        {
            Debug.LogFormat("左摇杆 长按");
        }
        #endregion

        #region 知识点三 手柄方向键
        //对应手柄上4个方向键 上下左右
        // gamepad.dpad.left
        // gamepad.dpad.right
        // gamepad.dpad.up
        // gamepad.dpad.down
        // 方向键-左-按下
        if (gamepad.dpad.left.wasPressedThisFrame)
        {
            Debug.LogFormat("方向键-左-按下");
        }
        // 方向键-左-抬起
        if (gamepad.dpad.left.wasReleasedThisFrame)
        {
            Debug.LogFormat("方向键-左-抬起");
        }
        // 方向键-左-长按
        if (gamepad.dpad.left.isPressed)
        {
            Debug.LogFormat("方向键-左-长按");
        }
        #endregion

        #region 知识点四 手柄右侧按键
        //通用
        //Y-△
        // gamepad.buttonNorth
        //A-X
        // gamepad.buttonSouth
        //X-□
        // gamepad.buttonWest
        //B-O
        // gamepad.buttonEast
        if (gamepad.buttonNorth.wasPressedThisFrame)
        {
            Debug.LogFormat("手柄右侧按键-Y(△)-按下");
        }
        // 方向键-左-抬起
        if (gamepad.buttonNorth.wasReleasedThisFrame)
        {
            Debug.LogFormat("手柄右侧按键-Y(△)-抬起");
        }
        // 方向键-左-长按
        if (gamepad.buttonNorth.isPressed)
        {
            Debug.LogFormat("手柄右侧按键-Y(△)-长按");
        }
        //手柄右侧按钮 X o △ □ A B Y(不推荐使用,使用东南西北更通用)
        //o
        // gamepad.circleButton
        //△
        // gamepad.triangleButton
        //□
        // gamepad.squareButton
        //X
        // gamepad.crossButton
        //x
        // gamepad.xButton
        //A
        // gamepad.aButton
        //B
        // gamepad.bButton
        //Y
        // gamepad.yButton

        #endregion

        #region 知识点五 手柄中央按键
        //中央键
        // gamepad.startButton
        // gamepad.selectButton
        //中央键start按下
        if (gamepad.startButton.wasPressedThisFrame)
        {
            Debug.LogFormat("中央键 start 按下");
        }
        //中央键start抬起
        if (gamepad.startButton.wasReleasedThisFrame)
        {
            Debug.LogFormat("中央键 start 抬起");
        }
        //中央键start长按
        if (gamepad.startButton.isPressed)
        {
            Debug.LogFormat("中央键 start 长按");
        }
        #endregion

        #region 知识点六 手柄肩部按键
        //左上右上 肩部键位
        //左右前方 肩部键
        // gamepad.leftShoulder
        // gamepad.rightShoulder
        //左侧肩部前方键
        if (gamepad.leftShoulder.wasPressedThisFrame)
        {
            Debug.LogFormat("左侧肩部前方键 按下");
        }
        //左侧肩部前方键
        if (gamepad.leftShoulder.wasReleasedThisFrame)
        {
            Debug.LogFormat("左侧肩部前方键 抬起");
        }
        //左侧肩部前方键
        if (gamepad.leftShoulder.isPressed)
        {
            Debug.LogFormat("左侧肩部前方键 长按");
        }

        //左右后方触发键
        // gamepad.leftTrigger
        // gamepad.rightTrigger
        //左侧肩部后方键
        if (gamepad.leftTrigger.wasPressedThisFrame)
        {
            Debug.LogFormat("左侧肩部前方键 按下");
        }
        //左侧肩部后方键
        if (gamepad.leftTrigger.wasReleasedThisFrame)
        {
            Debug.LogFormat("左侧肩部后方键 抬起");
        }
        //左侧肩部后方键
        if (gamepad.leftTrigger.isPressed)
        {
            Debug.LogFormat("左侧肩部后方键 长按");
        }
        #endregion
    }
}
